﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DSInternals.Common.Data;
using DSInternals.Common.Cryptography;

namespace DSInternals.Common.Test
{
    [TestClass]
    public class SupplementalCredentialsTester
    {
        [TestMethod]
        public void SupplementalCredentials_Parse_ADAM()
        {
            // AD LDS / ADAM has a slightly different structure of supplemental credentials
            byte[] input = "0100000001000000e80100000600000001000000e00100003100011d000000000000000000000000af4156909297baece4e553b731d9d552da58c12e5880cad54a3f909c07b0792eec9d262783c811ad9cc7aeb1ab019fe0af4156909297baece4e553b731d9d552af4156909297baece4e553b731d9d552ec9d262783c811ad9cc7aeb1ab019fe0da58c12e5880cad54a3f909c07b0792e95e11e920c7405ca6639d932888cc11c53d9e953141b322f29eb1a02798c299042eea90067734b2a81abbc79618cc1519d9aac28e63107a495266e755f7a2babac6b5dfb3bfa3b7039058310d5b36ccc7c97824dd59b9a6a053b5fae3d8603f20a5e2bc3111c759efb3a91e3740633e83b12daad09e83cb92477ac59993c5a843b12daad09e83cb92477ac59993c5a843b12daad09e83cb92477ac59993c5a8471e7228faba51df979beef06b34f791820aa1475f40540f0db59f9d2d93fc3da5f88f6d82d185741a82c7fa79e801c6d8eae592a0ce55271a17de0b39cf1e5e9923354fb99887724cf2b10e88108776b427804b8a6d6ed92c11ecb94e40da72c0c997bc1906b1f0092d61bd0efee428b0c997bc1906b1f0092d61bd0efee428b0c997bc1906b1f0092d61bd0efee428bd93e9a9a7452dff036a67a5d5d333d7c4effe21b0afc142382943ef26024649aaa6a6a95c237f2b8efa2f13ecb0cb220".HexToBinary();
            throw new AssertInconclusiveException("ADAM SupplementalCredentials parser is not yet implemented.");
            var result = new SupplementalCredentials(input);
        }

        [TestMethod]
        public void SupplementalCredentials_Parse_Null()
        {
            byte[] input = null;
            var result = new SupplementalCredentials(input);
        }

        [TestMethod]
        public void SupplementalCredentials_Parse_Empty1()
        {
            // Test 13B empty structure
            byte[] input = "00000000000000000000000000".HexToBinary();
            var result = new SupplementalCredentials(input);
        }

        [TestMethod]
        public void SupplementalCredentials_Parse_Empty2()
        {
            // Test 111B empty structure
            byte[] emptyCredentials = "000000006200000000000000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000500000".HexToBinary();
            var result = new SupplementalCredentials(emptyCredentials);
        }

        [TestMethod]
        public void SupplementalCredentials_Create_Empty()
        {
            var result = new SupplementalCredentials();
            string emptyCredentials = "000000006200000000000000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000500000";
            Assert.AreEqual(emptyCredentials, result.ToByteArray().ToHex());
        }

        [TestMethod]
        public void SupplementalCredentials_Parse_W2k16_Vector1()
        {
            byte[] blob = "00000000780a000000000000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000500005003200200001005000720069006d006100720079003a004e0054004c004d002d005300740072006f006e0067002d004e0054004f005700460039303534356562346361653431363336386630313965353965373765383535313600980301005000720069006d006100720079003a004b00650072006200650072006f0073002d004e0065007700650072002d004b0065007900730030343030303030303033303030303030303330303033303031633030316330303038303130303030303031303030303030303030303030303030303030303030303031303030303031323030303030303230303030303030323430313030303030303030303030303030303030303030303031303030303031313030303030303130303030303030343430313030303030303030303030303030303030303030303031303030303030333030303030303038303030303030353430313030303030303030303030303030303030303030303031303030303031323030303030303230303030303030356330313030303030303030303030303030303030303030303031303030303031313030303030303130303030303030376330313030303030303030303030303030303030303030303031303030303030333030303030303038303030303030386330313030303030303030303030303030303030303030303031303030303031323030303030303230303030303030393430313030303030303030303030303030303030303030303031303030303031313030303030303130303030303030623430313030303030303030303030303030303030303030303031303030303030333030303030303038303030303030633430313030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303034313030343430303431303035343030353530303464303032653030343330303466303034643030373430303635303037333030373430303066346562363736313034616431666361633961386662396632613139643634373763353263613538346234626536383934383531643436396266313034396361343737626238323665393864383634363566663439313562383539633663643435333832306461383362366436346130663465623637363130346164316663616339613866623966326131396436343737633532636135383462346265363839343835316434363962663130343963613437376262383236653938643836343635666634393135623835396336636434353338323064613833623664363461306634656236373631303461643166636163396138666239663261313964363437376335326361353834623462653638393438353164343639626631303439636134373762623832366539386438363436356666343931356238353963366364343533383230646138336236643634612000f00001005000720069006d006100720079003a004b00650072006200650072006f0073003033303030303030303130303031303031633030316330303463303030303030303030303030303030303030303030303033303030303030303830303030303036383030303030303030303030303030303030303030303030333030303030303038303030303030373030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030343130303434303034313030353430303535303034643030326530303433303034663030346430303734303036353030373330303734303034353338323064613833623664363461343533383230646138336236643634611000d80002005000610063006b0061006700650073003465303035343030346330303464303032643030353330303734303037323030366630303665303036373030326430303465303035343030346630303537303034363030303030303462303036353030373230303632303036353030373230303666303037333030326430303465303036353030373730303635303037323030326430303462303036353030373930303733303030303030346230303635303037323030363230303635303037323030366630303733303030303030353730303434303036393030363730303635303037333030373430301e00c00301005000720069006d006100720079003a00570044006900670065007300740033313030303131643030303030303030303030303030303030303030303030303338373233656433346436646638346662383839373263653030623964323839353265393534623166393233363864343066623838633962353464663264356165323735616565626536643061306461376230353732663731396262333037623338373233656433346436646638346662383839373263653030623964323839353265393534623166393233363864343066623838633962353464663264356166653933633663633631303238626464393362373430343265643737386332333338373233656433346436646638346662383839373263653030623964323839343239303732626666613562653037353938636134336165353238333337333234323930373262666661356265303735393863613433616535323833333733323037343963356363666333666364396132336638656533373366653064616563353531313335313664666433323634303330323962626335363862326661346134323930373262666661356265303735393863613433616535323833333733326430343632323736326466313364623930373836623732633432333031376462353531313335313664666433323634303330323962626335363862326661346163333566356139346331663762643963306236353464613435323666326461356333356635613934633166376264396330623635346461343532366632646135396332303064323066613164313866383134626434393131303865336635343430373834653064643932626564353330346134353839626336373531646438633134386463376265303237306334363533396362633961366333353039653236393166353264633665636266336366326231643830326562643965353633313964303965346136326363663633386332623861643133363062656264313163656430396534613632636366363338633262386164313336306265626431316365646235363565363433643138643263336262346633646139343262323764646335366430646139373631663965316264363033333262333635613130363737323536643064613937363166396531626436303333326233363561313036373732356638613961313836333664373838663536633433326136653531666538383466626239626236393339643339643232653137646363666662616236663331346663333161666330653063373238333963353166353634376431353832623833616563643337303738323731363661653264353564613861346632613864386200".HexToBinary();
            var credentials = new SupplementalCredentials(blob);
            byte[] blob2 = credentials.ToByteArray();
            Assert.AreEqual(blob.Length, blob2.Length);
            Assert.AreEqual(blob.ToHex(), blob2.ToHex());
        }

        [TestMethod]
        public void SupplementalCredentials_Parse_W2k16_Vector2()
        {
            byte[] blob = "000000005008000000000000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000500005003200200001005000720069006d006100720079003a004e0054004c004d002d005300740072006f006e0067002d004e0054004f005700460031643638373531326339333534646365653233376665613930643139356363373600a00101005000720069006d006100720079003a004b00650072006200650072006f0073002d004e0065007700650072002d004b0065007900730030343030303030303033303030303030303030303030303032303030323030303738303030303030303031303030303030303030303030303030303030303030303031303030303031323030303030303230303030303030393830303030303030303030303030303030303030303030303031303030303031313030303030303130303030303030623830303030303030303030303030303030303030303030303031303030303030333030303030303038303030303030633830303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303034313030343430303431303035343030353530303464303032653030343330303466303034643030373530303733303036353030373230303330303033323030363232666361316431326465316466363565383430616664356633623938343462313861373261383261656436653365353863333932353737353366646661383330363636393734636237323965663662643434663865616233323936653563313366386664333764353537613430312000c00001005000720069006d006100720079003a004b00650072006200650072006f0073003033303030303030303130303030303032303030323030303338303030303030303030303030303030303030303030303033303030303030303830303030303035383030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303034313030343430303431303035343030353530303464303032653030343330303466303034643030373530303733303036353030373230303330303033323030313366386664333764353537613430311000d80002005000610063006b0061006700650073003465303035343030346330303464303032643030353330303734303037323030366630303665303036373030326430303465303035343030346630303537303034363030303030303462303036353030373230303632303036353030373230303666303037333030326430303465303036353030373730303635303037323030326430303462303036353030373930303733303030303030346230303635303037323030363230303635303037323030366630303733303030303030353730303434303036393030363730303635303037333030373430301e00c00301005000720069006d006100720079003a00570044006900670065007300740033313030303131643030303030303030303030303030303030303030303030303963333836656664383134663734396365653066326139653665643765633633326438343535393963346266623533333732303730623362666132646339323235633063353839333430356536353138333432323132663864393564363837643963333836656664383134663734396365653066326139653665643765633633326438343535393963346266623533333732303730623362666132646339323238353037393131323334326532363433303162636566643232376536663166663963333836656664383134663734396365653066326139653665643765633633323338383064366134316161336163333730313565383262653066623033363063633634383439353232643735313239303264653236313230303431303230623934666234646333646230386133663935343731366436643264366563363062633333393036653139623034323263376638366463363866326536356165323863633634383439353232643735313239303264653236313230303431303230623536663030336262343532616633666634643163373464363464643035363139633333393036653139623034323263376638366463363866326536356165323861666437346463366633376132373732663038396364636166326436663961626366333139366136383137313439363066366463393237646137363864666263323237386336656533643064336438613261343635336666306562396630623039333962666432656338623131656262386531326563636363613766356131393538613934613463326134633262343839373230313831346235626238666230363763663335336665636232343335356466353538653837626534353432386261336634346233613037346334666364636261623934653635303533363166376133663434623361303734633466636463626162393465363530353336316637393265633665646235336238366139326634363965326339336433356135616430633331313966636536386133653331353134323131363739366531396638353462333938616139326536663633333163353630633662633939333264386436363163316136653930373937306434313662353538626563366164373736383937653439356362306334376130356661646130626638656362656130316634333965356436353337383164343366323734346535343838663762623762323838633066623339303139343864393837303766316161653436346165643563343001".HexToBinary();
            var credentials = new SupplementalCredentials(blob);
            byte[] blob2 = credentials.ToByteArray();
            Assert.AreEqual(blob.Length, blob2.Length);
            Assert.AreEqual(blob.ToHex(), blob2.ToHex());
        }

        [TestMethod]
        public void SupplementalCredentials_Parse_W2k16_Vector3()
        {
            byte[] blob = "000000008809000000000000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000500005003200200001005000720069006d006100720079003a004e0054004c004d002d005300740072006f006e0067002d004e0054004f005700460064626533343361626337656632643162313166366136643537666437393636383600a00201005000720069006d006100720079003a004b00650072006200650072006f0073002d004e0065007700650072002d004b006500790073003034303030303030303330303030303030333030303030303230303032303030633030303030303030303130303030303030303030303030303030303030303030303130303030303132303030303030323030303030303065303030303030303030303030303030303030303030303030303130303030303131303030303030313030303030303030303031303030303030303030303030303030303030303030303130303030303033303030303030303830303030303031303031303030303030303030303030303030303030303030303130303030303132303030303030323030303030303031383031303030303030303030303030303030303030303030303130303030303131303030303030313030303030303033383031303030303030303030303030303030303030303030303130303030303033303030303030303830303030303034383031303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303431303034343030343130303534303035353030346430303265303034333030346630303464303037353030373330303635303037323030333030303333303037646266326238323237313934353066633533303531343334626438336361616433613436323462326166316538396461633266353665336438316436663937633362653262636439616533336665643862383338383463643032386532353936326539363765363062643637356561376462663262383232373139343530666335333035313433346264383363616164336134363234623261663165383964616332663536653364383164366639376333626532626364396165333366656438623833383834636430323865323539363265393637653630626436373565612000f80001005000720069006d006100720079003a004b00650072006200650072006f00730030333030303030303031303030313030323030303230303034633030303030303030303030303030303030303030303030333030303030303038303030303030366330303030303030303030303030303030303030303030303330303030303030383030303030303734303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303431303034343030343130303534303035353030346430303265303034333030346630303464303037353030373330303635303037323030333030303333303036326539363765363062643637356561363265393637653630626436373565611000d80002005000610063006b0061006700650073003465303035343030346330303464303032643030353330303734303037323030366630303665303036373030326430303465303035343030346630303537303034363030303030303462303036353030373230303632303036353030373230303666303037333030326430303465303036353030373730303635303037323030326430303462303036353030373930303733303030303030346230303635303037323030363230303635303037323030366630303733303030303030353730303434303036393030363730303635303037333030373430301e00c00301005000720069006d006100720079003a00570044006900670065007300740033313030303131643030303030303030303030303030303030303030303030303033666330316465373131623033333135386130363038343037343538393034373431363466323265653166303435326136373739623432393031613835306163343437393533313466343738363639313362366464663230386236633966323033666330316465373131623033333135386130363038343037343538393034373431363466323265653166303435326136373739623432393031613835306136353165383630313533313961626163366262393430326531663734323166323033666330316465373131623033333135386130363038343037343538393034383863363563336635653232336461366662386638663465326163303463663239653363613966353463656566323431623733303737333566366363656430336335396365613336353464656464653466363463356236373662306665636165303035613437313931353136366563333065383862623837323833353733626639653363613966353463656566323431623733303737333566366363656430333732613561323461326234653337616562616334323639336233303237326331303035613437313931353136366563333065383862623837323833353733626631323137336233363932663337346233303432306262646632346234356539633631303263396561656334333932396536373031313365323532316565663939323438383132306339386637333638653539326561663164613233636661303663353465333139303763363631353431646264303864323362326339613530613461666431643934656636363932333163313462393339326537373937653762336266333936633530353338356566653139363261643930303963373231393538636137303462616534623538303863616639313862343930356165616331393863613730346261653462353830386361663931386234393035616561633139663230346164333262663665343466383837643066666666326437626539633236633432373937373061346565343762633465396231623938633134613832313835366539613133666634313337346432656336326365666234396237306664306634653636393032626236653564363530303963313061656338343762643435383432363833383731376238353831323436353136356262373631366132636463383031623063333762623034643964346364393463343863613634613861366334663632366135353334616337666330333161313736623563383366363800".HexToBinary();
            var credentials = new SupplementalCredentials(blob);
            byte[] blob2 = credentials.ToByteArray();
            Assert.AreEqual(blob.Length, blob2.Length);
            Assert.AreEqual(blob.ToHex(), blob2.ToHex());
        }

        [TestMethod]
        public void SupplementalCredentials_Parse_W2k16_Vector4()
        {
            byte[] blob = "00000000880a000000000000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000500005003200200001005000720069006d006100720079003a004e0054004c004d002d005300740072006f006e0067002d004e0054004f005700460064613664376662373363666261323738336233323836333563303130613163343600a00301005000720069006d006100720079003a004b00650072006200650072006f0073002d004e0065007700650072002d004b00650079007300303430303030303030333030303030303033303030333030323030303230303030383031303030303030313030303030303030303030303030303030303030303030313030303030313230303030303032303030303030303238303130303030303030303030303030303030303030303030313030303030313130303030303031303030303030303438303130303030303030303030303030303030303030303030313030303030303330303030303030383030303030303538303130303030303030303030303030303030303030303030313030303030313230303030303032303030303030303630303130303030303030303030303030303030303030303030313030303030313130303030303031303030303030303830303130303030303030303030303030303030303030303030313030303030303330303030303030383030303030303930303130303030303030303030303030303030303030303030313030303030313230303030303032303030303030303938303130303030303030303030303030303030303030303030313030303030313130303030303031303030303030306238303130303030303030303030303030303030303030303030313030303030303330303030303030383030303030306338303130303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030343130303434303034313030353430303535303034643030326530303433303034663030346430303735303037333030363530303732303033303030333430306232326636303466343463666332653263613432316636656333613931646433616637333031396233303134383564343662373566316163303266626433353665383031333136306330333862613566363762363966663739363365316162306338366233313362626338396235663164656438353334393062316664653339343036623733623631356130633065616432343430316436326237396639623737643162656530633330666639373564313465306238383235613032643239343163336330653661353536306639373430313733306564393037363435313565623232663630346634346366633265326361343231663665633361393164643361663733303139623330313438356434366237356631616330326662643335366538303133313630633033386261356636376236396666373936336531616230633836623331336262633839623566312000f80001005000720069006d006100720079003a004b00650072006200650072006f00730030333030303030303031303030313030323030303230303034633030303030303030303030303030303030303030303030333030303030303038303030303030366330303030303030303030303030303030303030303030303330303030303030383030303030303734303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303431303034343030343130303534303035353030346430303265303034333030346630303464303037353030373330303635303037323030333030303334303063383662333133626263383962356631303137333065643930373634353135651000d80002005000610063006b0061006700650073003465303035343030346330303464303032643030353330303734303037323030366630303665303036373030326430303465303035343030346630303537303034363030303030303462303036353030373230303632303036353030373230303666303037333030326430303465303036353030373730303635303037323030326430303462303036353030373930303733303030303030346230303635303037323030363230303635303037323030366630303733303030303030353730303434303036393030363730303635303037333030373430301e00c00301005000720069006d006100720079003a00570044006900670065007300740033313030303131643030303030303030303030303030303030303030303030303637333137633437393534346464323762313137383463373161353531306264626336343935313933323233646330326533353762303965363961636438626638366630356134613564663462616165303161623931663562663131653561613637333137633437393534346464323762313137383463373161353531306264626336343935313933323233646330326533353762303965363961636438626632323661313034643065666663643035333032613965626235383263633565643637333137633437393534346464323762313137383463373161353531306264643031633237633061643761626437663765616161356265303165303463616138653634333838306334343132303130383865373565396438313331303064313639656538653762343039323462383232633339386132636430333036636631393337636235353263363635306363306639366632366333363837306137623838653634333838306334343132303130383865373565396438313331303064313439336231666231336237353163376236623135353839376161393834383937393337636235353263363635306363306639366632366333363837306137623831623135623766396239326130326634306331343561313736353364343263613234383363633234623336346263343264313035626636343936623739643064353166353631313736343438663766643637626535356462353832653536333862333631623231656162376262356632313566653265356237666665643933333730306661323236616538653630343137316436373438643961643231633464313931613030306337383531656436663237306264383739383166623834343839303934663663353533653831643030376266313734646566383366383465303930393466366335353365383164303037626631373464656638336638346530366238636465376439653937386333333061323833633862393261623664613262393631316566613839643833323737666435643430626331393565303835383934356566303663376361346339636366383031393063363839613333303933333237666463613135353662316230303836656563366666613338313565303233333763663537663437613435306436386265343665386665383438396261376163663133653463316639303265306161313438623837356366373761663535356663386665373065316434656461626439653663393038333431386233363601".HexToBinary();
            var credentials = new SupplementalCredentials(blob);
            byte[] blob2 = credentials.ToByteArray();
            Assert.AreEqual(blob.Length, blob2.Length);
            Assert.AreEqual(blob.ToHex(), blob2.ToHex());
        }

        [TestMethod]
        public void SupplementalCredentials_Parse_W2k16_Vector5()
        {
            byte[] blob = "00000000b00a000000000000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000500004003600e00301005000720069006d006100720079003a004b00650072006200650072006f0073002d004e0065007700650072002d004b0065007900730030343030303030303033303030303030303330303033303034303030343030303038303130303030303031303030303030303030303030303030303030303030303031303030303031323030303030303230303030303030343830313030303030303030303030303030303030303030303031303030303031313030303030303130303030303030363830313030303030303030303030303030303030303030303031303030303030333030303030303038303030303030373830313030303030303030303030303030303030303030303031303030303031323030303030303230303030303030383030313030303030303030303030303030303030303030303031303030303031313030303030303130303030303030613030313030303030303030303030303030303030303030303031303030303030333030303030303038303030303030623030313030303030303030303030303030303030303030303031303030303031323030303030303230303030303030623830313030303030303030303030303030303030303030303031303030303031313030303030303130303030303030643830313030303030303030303030303030303030303030303031303030303030333030303030303038303030303030653830313030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303034313030343430303431303035343030353530303464303032653030343330303466303034643030363830303666303037333030373430303663303036663030366530303264303036343030363330303331303032653030363130303634303036313030373430303735303036643030326530303633303036663030366430303833643563663533353265393033356565366539666266373832663630626333393866353233643263306130646538616438366262613733313932643664353130303435663633353064393462616462383332666436333162336633663938633730393163653835343536313364333165396163646531313539323530316561333839643564646161353832656133363562356238366133363163336235333239333632323330656266366464303065393836653333663338356538363661636334333663623938633132363930306461346364353765613062336434303461613335363436643936316261386539346535326165303266366631383135613530663961613061346261656337623138356166386665623333383834636533333938636635633866376430653637613366643466393730333861373431383465646131306137326166383763316630312000380101005000720069006d006100720079003a004b00650072006200650072006f0073003033303030303030303130303031303034303030343030303463303030303030303030303030303030303030303030303033303030303030303830303030303038633030303030303030303030303030303030303030303030333030303030303038303030303030393430303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030343130303434303034313030353430303535303034643030326530303433303034663030346430303638303036663030373330303734303036633030366630303665303032643030363430303633303033313030326530303631303036343030363130303734303037353030366430303265303036333030366630303664303037303931636538353435363133643331613463643537656130623364343034611000d80002005000610063006b0061006700650073003465303035343030346330303464303032643030353330303734303037323030366630303665303036373030326430303465303035343030346630303537303034363030303030303462303036353030373230303632303036353030373230303666303037333030326430303465303036353030373730303635303037323030326430303462303036353030373930303733303030303030346230303635303037323030363230303635303037323030366630303733303030303030353730303434303036393030363730303635303037333030373430301e00c00301005000720069006d006100720079003a00570044006900670065007300740033313030303131643030303030303030303030303030303030303030303030306438363437393236653065653832353832626262613132396238656237613166323434383964613765646439313735343838343734373930343766623666313864383634373932366530656538323538326262626131323962386562376131666438363437393236653065653832353832626262613132396238656237613166656535383833633133306131303930353032633030363436356465323663616365653538383363313330613130393035303263303036343635646532366361633333333530393334333839303064386238303365643061616566646162373363303763626232323739656439386539663436376564653031636164636564386530306664623765333137393837343566303637626437366232346535663732336362346134646639366438363065303430623238313431353936663734373163636234613464663936643836306530343062323831343135393666373437316362346161643963303962656362303865313834646632643162303430396130326234616164396330396265636230386531383464663264316230343039613032656537373831323230303463316239343966363364373839313133643839366333336537386333653938326633333038373764353534323038373336383431396565356133383663376462373361633836366230636432353237323763303162613339343663346562666435653762303662616461303830393336313163323932393534353537386136656163376262366234383136366231626535666538346335393364643562643666366239666435633366643133336236333536613763323935343535373861366561633762623662343831363662316265356665383466623366636165323434333861303463393765363731306234353763333061383132613237623538646238366438633433346130313365663736303863656536666233666361653234343338613034633937653637313062343537633330613832353736323134653635323131626462336632656465656633336234346634333364353539616633653431646666383261343338646234613433643533613637363138353362353134333636313137643566323133313066633164343266396662626535363966663137616564666666326632396465333836633863623366386532343761343630346432346237303136656162303135323231663762313136626265353639666631376165646666663266323964653338366338636233663800".HexToBinary();
            var credentials = new SupplementalCredentials(blob);
            byte[] blob2 = credentials.ToByteArray();
            // Note that we do not test the serialized value for equality, because unlike Windows Server 2016, our implementation does not put the NTLM-Strong-NTOWF property name to the Properties list if this package is not actually present.
        }

        [TestMethod]
        public void SupplementalCredentials_Parse_W2k3_Vector1()
        {
            byte[] blob = "000000000406000000000000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000500003002000400101005000720069006d006100720079003a004b00650072006200650072006f00730030333030303030303032303030303030333030303330303035303030303030303030303030303030303030303030303030333030303030303038303030303030383030303030303030303030303030303030303030303030303130303030303030383030303030303838303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030343330303466303034653030353430303466303035333030346630303265303034333030346630303464303034313030363430303664303036393030366530303639303037333030373430303732303036313030373430303666303037323030616564303263353232303463613263656165643032633532323034636132636530303030303030303030303030303030303030303030303030303030303030301000400002005000610063006b006100670065007300346230303635303037323030363230303635303037323030366630303733303030303030353730303434303036393030363730303635303037333030373430301e00c00301005000720069006d006100720079003a00570044006900670065007300740033313030303131643030303030303030303030303030303030303030303030303431313563366234376637373561306136663034353830666531323634346635643336363162336461396134366665303931316335303261636231636465363463643834393763316532626331636461656664343662613162326663613439313431313563366234376637373561306136663034353830666531323634346635656134383731323434626665316536396538326432636562363666326132313664343338643964306161626666343830306531313434316166633530303437336137623662373932336139323630333530303538623335656263356537656138343135356530343563623837373038656539663130613261363539626363363566303136313039343636336264653235343333666430653162643637653862626562356662386265336430363435653330366362353865663162323062373537396136303435346463663837376562633635393031623661393338633536633934313535653034356362383737303865653966313061326136353962636336356162633162323261306565363361383233303632653666623966393639333661353061323736383435356331666662386231333863613064663633636635326136343338343863333661333262386162383736656462333334303638663239636134306338656337356230373038303866313234336263616165653364356462393139333838616238326132373936626633306331356466373632343435666439666331343861623839313163663434393663653137393331303432366336663232396139613166353438393530396633383962613865643739353330626339653464386230303037346466366133366465313830643163396138643863633638623863346633333964333733366564653263346330626334653533663366663230396165336231646336373839663930663335326532666632373331313162396232326461653234356238326333653535303631323635623238383630376332366236646638373236333034366132313363363534396439383233383363663938656437386465303531353164636533393538666539643435393638313032343062306434303562383737336566323465323964343638356564383261353164353832633136366432656639616232623233616262383730383062383535646632663532636132383461356637663262323238623464333035386466303832373234396433366566316636386163393363643331373330336336393866663800".HexToBinary();
            var credentials = new SupplementalCredentials(blob);
            byte[] blob2 = credentials.ToByteArray();
            // Note that we do not test the serialized value for equality, because Windows Server 2003 uses different credential paddings.
        }

        [TestMethod]
        public void SupplementalCredentials_Parse_W2k3_Vector2()
        {
            byte[] blob = "00000000a806000000000000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000500003002000e40101005000720069006d006100720079003a004b00650072006200650072006f007300303330303030303030323030303230303461303034613030373830303030303030303030303030303030303030303030303330303030303030383030303030306332303030303030303030303030303030303030303030303031303030303030303830303030303063613030303030303030303030303030303030303030303030333030303030303038303030303030643230303030303030303030303030303030303030303030303130303030303030383030303030306461303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030343330303466303034653030353430303466303035333030346630303265303034333030346630303464303036383030366630303733303037343030373730303639303036653030333230303662303033333030373230303332303036353030363530303265303036333030366630303665303037343030366630303733303036663030326530303633303036663030366430306439623333656230363465333835646664396233336562303634653338356466663139316539613762353631353235646631393165396137623536313532356430303030303030303030303030303030303030303030303030303030303030301000400002005000610063006b006100670065007300346230303635303037323030363230303635303037323030366630303733303030303030353730303434303036393030363730303635303037333030373430301e00c00301005000720069006d006100720079003a00570044006900670065007300740033313030303131643030303030303030303030303030303030303030303030303962643638616265363465383266666264376639393666343736633161613663363565623965326331386363363961306233633031326136353763323264623739626436386162653634653832666662643766393936663437366331616136633962643638616265363465383266666264376639393666343736633161613663626133343231303165303331653165653630366439633436313535366538623662613334323130316530333165316565363036643963343631353536653862366237336338396333346532643661643162353335333362393733383538653165633133373331393034373534393966616261633636343336656234643966343235633232633233393532643432643565393531396335303361326235303832386135333838393633656234386136663466386231623734393361323262366631613533383839363365623438613666346638623162373439336132326236663163313337333139303437353439396661626163363634333665623464396634326331333733313930343735343939666162616336363433366562346439663432313534313936313665323663383738623363666439356366653030306266666663363261333366336136613135356562363339393531356535363638623863653936643964336137373739383633393037386433306365623539633538376365306662626363633833643237373739336630633331313637353366306535653534376234663561336638336666383439636433656661633863343761333637313335386234656239393732393434633235636432663631613565616133626631343762346635613366383366663834396364336566616338633437613336373161343632323165626135623463623930323731343933333831663934353935333334636261613034616238386634326562613536376135636134393837623936613436323231656261356234636239303237313439333338316639343539353333383432393939353465303561613636366233646665353138313164626466373762636536313366376264383437656465643232386335343137303664376633396533376339333533613133656563396666633439663665386634653639613563393932353633663566363631396665303537326234336532663066373934363630303363303631636634666437623836346461356535316165346365623465633939323536336635663636313966653035373262343365326630663739343672".HexToBinary();
            var credentials = new SupplementalCredentials(blob);
            byte[] blob2 = credentials.ToByteArray();
            // Note that we do not test the serialized value for equality, because Windows Server 2003 uses different credential paddings.
        }

        [TestMethod]
        public void SupplementalCredentials_Generate1()
        {
            var credentials = new SupplementalCredentials(@"Pa$$w0rd".ToSecureString(), "Administrator", "Administrator@Adatum.com", "ADATUM", "Adatum.com");
            byte[] blob = credentials.ToByteArray();
            var credentials2 = new SupplementalCredentials(blob);

            // Test integrity
            Assert.AreEqual(credentials2.ClearText, credentials.ClearText);
            Assert.AreEqual(credentials2.NTLMStrongHash.Length, credentials.NTLMStrongHash.Length);

            // Test WDigest
            Assert.AreEqual(credentials2.WDigest.Length, credentials.WDigest.Length);
            Assert.AreEqual(credentials2.WDigest[0].ToHex(), credentials.WDigest[0].ToHex());

            // Test Kerberos
            Assert.AreEqual(credentials2.Kerberos.DefaultSalt, credentials.Kerberos.DefaultSalt);
            Assert.AreEqual(credentials2.Kerberos.Credentials[0].KeyType, credentials.Kerberos.Credentials[0].KeyType);

            // Test key serialization
            Assert.AreEqual(credentials2.Kerberos.ToByteArray().ToHex(), credentials.Kerberos.ToByteArray().ToHex());
            Assert.AreEqual(credentials2.KerberosNew.ToByteArray().ToHex(), credentials.KerberosNew.ToByteArray().ToHex());
        }

        [TestMethod]
        public void SupplementalCredentials_Generate2()
        {
            var credentials = new SupplementalCredentials(@"Pa$$w0rd".ToSecureString(), "user02", null, "ADATUM", "Adatum.com");
            byte[] blob = credentials.ToByteArray();

            byte[] expectedBlob = "000000005008000000000000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000500005003200200001005000720069006d006100720079003a004e0054004c004d002d005300740072006f006e0067002d004e0054004f005700460031643638373531326339333534646365653233376665613930643139356363373600a00101005000720069006d006100720079003a004b00650072006200650072006f0073002d004e0065007700650072002d004b0065007900730030343030303030303033303030303030303030303030303032303030323030303738303030303030303031303030303030303030303030303030303030303030303031303030303031323030303030303230303030303030393830303030303030303030303030303030303030303030303031303030303031313030303030303130303030303030623830303030303030303030303030303030303030303030303031303030303030333030303030303038303030303030633830303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303034313030343430303431303035343030353530303464303032653030343330303466303034643030373530303733303036353030373230303330303033323030363232666361316431326465316466363565383430616664356633623938343462313861373261383261656436653365353863333932353737353366646661383330363636393734636237323965663662643434663865616233323936653563313366386664333764353537613430312000c00001005000720069006d006100720079003a004b00650072006200650072006f0073003033303030303030303130303030303032303030323030303338303030303030303030303030303030303030303030303033303030303030303830303030303035383030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303034313030343430303431303035343030353530303464303032653030343330303466303034643030373530303733303036353030373230303330303033323030313366386664333764353537613430311000d80002005000610063006b0061006700650073003465303035343030346330303464303032643030353330303734303037323030366630303665303036373030326430303465303035343030346630303537303034363030303030303462303036353030373230303632303036353030373230303666303037333030326430303465303036353030373730303635303037323030326430303462303036353030373930303733303030303030346230303635303037323030363230303635303037323030366630303733303030303030353730303434303036393030363730303635303037333030373430301e00c00301005000720069006d006100720079003a00570044006900670065007300740033313030303131643030303030303030303030303030303030303030303030303963333836656664383134663734396365653066326139653665643765633633326438343535393963346266623533333732303730623362666132646339323235633063353839333430356536353138333432323132663864393564363837643963333836656664383134663734396365653066326139653665643765633633326438343535393963346266623533333732303730623362666132646339323238353037393131323334326532363433303162636566643232376536663166663963333836656664383134663734396365653066326139653665643765633633323338383064366134316161336163333730313565383262653066623033363063633634383439353232643735313239303264653236313230303431303230623934666234646333646230386133663935343731366436643264366563363062633333393036653139623034323263376638366463363866326536356165323863633634383439353232643735313239303264653236313230303431303230623536663030336262343532616633666634643163373464363464643035363139633333393036653139623034323263376638366463363866326536356165323861666437346463366633376132373732663038396364636166326436663961626366333139366136383137313439363066366463393237646137363864666263323237386336656533643064336438613261343635336666306562396630623039333962666432656338623131656262386531326563636363613766356131393538613934613463326134633262343839373230313831346235626238666230363763663335336665636232343335356466353538653837626534353432386261336634346233613037346334666364636261623934653635303533363166376133663434623361303734633466636463626162393465363530353336316637393265633665646235336238366139326634363965326339336433356135616430633331313966636536386133653331353134323131363739366531396638353462333938616139326536663633333163353630633662633939333264386436363163316136653930373937306434313662353538626563366164373736383937653439356362306334376130356661646130626638656362656130316634333965356436353337383164343366323734346535343838663762623762323838633066623339303139343864393837303766316161653436346165643563343001".HexToBinary();
            var expectedCredentials = new SupplementalCredentials(expectedBlob);

            // Test integrity
            Assert.AreEqual(expectedCredentials.ClearText, credentials.ClearText);
            Assert.AreEqual(expectedCredentials.NTLMStrongHash.Length, credentials.NTLMStrongHash.Length);
            Assert.AreEqual(WDigestHash.Encode(expectedCredentials.WDigest).ToHex(), WDigestHash.Encode(credentials.WDigest).ToHex());
            Assert.AreEqual(expectedCredentials.Kerberos.ToByteArray().ToHex(), credentials.Kerberos.ToByteArray().ToHex());
            Assert.AreEqual(expectedCredentials.KerberosNew.ToByteArray().ToHex(), credentials.KerberosNew.ToByteArray().ToHex());
        }
    }
}
